
const fs = require('fs')
const execa = require('execa')
// 获取命令行参数
const args = process.argv.slice(2);
let size = 20
// 解析命令行参数，并设置环境变量
for (const arg of args) {
  const [argName, argValue] = arg.split('=');
  if (argName === 'SIZE') {
    size = argValue;
  }
}
// 只针对文件夹进行打包
const dirs = fs
  .readdirSync('packages')
  .filter(dir => fs.statSync(`packages/${dir}`).isDirectory())
console.log(dirs)
// 进行打包，并行打包
const build = async target => {
  await execa('rollup', ['-c', '--environment', `TARGET:${target}`, '--environment', `NODE_ENV:production`, '--environment', `SIZE:${size}`], {
    stdio: 'inherit'
  })
}
const runParaller = async (dirs, itemfn) => {
  const result = []
  dirs.forEach(dir => {
    result.push(itemfn(dir))
  })
  return Promise.all(result)
}
runParaller(dirs, build).then(() => {
  console.log('打包完成')
})
